<?php
//分页函数
define('PHP_CLOSE_TAG', '?'.'>');
define('PHP_NEXTLINE', (PHP_OS == 'WINNT')?"\r\n":"\n");
function pagenav($num, $perpage, $curr_page, $mpurl,&$pagenav) {
	$multipage = '';
	$multi_size=5;
	if(strtolower(substr($mpurl,-4))==".php")
		$x="?";
	else
		$x="&";
		$offset = 2;
		$page = $multi_size;
		$pages = ceil($num / $perpage);
		if($curr_page==$pages)
			$from = $curr_page - $multi_size;
		else
			$from = $curr_page - $offset;
		$to = $from + $multi_size;
		if($from+$multi_size>$pages){
			$to=$pages;
			$from=$pages-$multi_size;
			}
		if($from<1){
			$from=1;
			$to=$multi_size;
		}
		if($to>$pages)
			$to=$pages;

		$multipage .=($from==1)?"":'[&nbsp;<a href="'.$mpurl.$x.'page=1">1</a>&nbsp;]'.($from>1?"...":"").'&nbsp;';
		for($i = $from; $i < $to; $i++) {
			if($i != $curr_page) {
				$multipage .= '[&nbsp;<a href="'.$mpurl.$x.'page='.$i.'">'.$i.'</a>&nbsp;]&nbsp;';
			} else {
				$multipage .= '[&nbsp;<b>'.$i.'</b>&nbsp;]&nbsp;';
			}
		}
		$multipage .= ($to<$pages?"... ":"")."[&nbsp;<a href=\"$mpurl".$x."page=$pages\">$pages</a>&nbsp;]";
	
	if($curr_page>1) $pagenav["backpage"]="<a href=\"".$mpurl.$x.'page='.($curr_page-1)."\">".$pagenav["backpage"]."</a>";
	if($curr_page<$pages) $pagenav["nextpage"]="<a href=\"".$mpurl.$x.'page='.($curr_page+1)."\">".$pagenav["nextpage"]."</a>";
	return $multipage;
	}

function multi2($num, $perpage, $curr_page, $mpurl,$multi_size=5) {
	$multipage = '';
	if(strtolower(substr($mpurl,-4))==".php")
		$x="?";
	else
		$x="&";

		$offset = 2;
		$page = $multi_size;
		$pages = ceil($num / $perpage);
		if($curr_page==$pages)
			$from = $curr_page - $multi_size;
		else
			$from = $curr_page - $offset;
		$to = $from + $multi_size;
		if($from+$multi_size>$pages){
			$to=$pages;
			$from=$pages-$multi_size;
			}
		if($from<1){
			$from=1;
			$to=$multi_size;
		}
		if($to>$pages)
			$to=$pages;

		$multipage .=($from==1)?"":'[&nbsp;<a href="'.$mpurl.$x.'page=1">1</a>&nbsp;?'.($from>1?"...":"").'&nbsp;';
		for($i = $from; $i < $to; $i++) {
			if($i != $curr_page) {
				$multipage .= '[&nbsp;<a href="'.$mpurl.$x.'page='.$i.'">'.$i.'</a>&nbsp;]&nbsp;';
			} else {
				$multipage .= '[&nbsp;<b>'.$i.'</b>&nbsp;]&nbsp;';
			}
		}
		$multipage .= ($to<$pages?"... ":"")."[&nbsp;<a href=\"$mpurl".$x."page=$pages\">$pages</a>&nbsp;]";

	return $multipage;
}

function multi($num, $perpage, $curr_page, $mpurl) {
	$multipage = '';
	if(strtolower(substr($mpurl,-4))==".php")
		$x="?";
	else
		$x="";

	if($num > $perpage) {
		$page = 10;
		$offset = 2;

		$pages = ceil($num / $perpage);
		$from = $curr_page - $offset;
		$to = $curr_page + $page - $offset - 1;
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			if($from < 1) {
				$to = $curr_page + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} elseif($to > $pages) {
				$from = $curr_page - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}
		$multipage .= '<a href="'.$mpurl.$x.'1">&lt;&lt;</a> &nbsp;';
		for($i = $from; $i <= $to; $i++) {
			if($i != $curr_page) {
				$multipage .= '<a href="'.$mpurl.$x.$i.'">['.$i.']</a>';
			} else {
				$multipage .= '<u><b>['.$i.']</b></u>&nbsp;';
			}
		}
		$multipage .= $pages > $page ? " ... <a href=\"$mpurl{$x}$pages\"> [$pages] &gt;&gt;</a>" : " <a href=\"$mpurl{$x}$pages\">&gt;&gt;</a>";
	}
	return $multipage;
}
function pmulti($num, $perpage, $curr_page, $mpurl) {
	$multipage = '';
	if($num > $perpage) {
		$page = 10;
		$offset = 2;
		$pages = ceil($num / $perpage);
		$from = $curr_page - $offset;
		$to = $curr_page + $page - $offset - 1;
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			if($from < 1) {
				$to = $curr_page + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} elseif($to > $pages) {
				$from = $curr_page - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}
		$multipage .= '<a href="'.$mpurl.'1">&lt;&lt;</a> &nbsp;';
		for($i = $from; $i <= $to; $i++) {
			if($i != $curr_page) {
				$multipage .= '<a href="'.$mpurl.$i.'">['.$i.']</a>&nbsp;';
			} else {
				$multipage .= '<u><b>['.$i.']</b></u>&nbsp;';
			}
		}
		$multipage .= $pages > $page ? " ... <a href=\"$mpurl"."$pages\"> [$pages] &gt;&gt;</a>" : " <a href=\"$mpurl"."$pages\">&gt;&gt;</a>";
	}
	return $multipage;
}

function imgMulti($num, $perpage, $curr_page, $mpurl) {
	$multipage = '';
	if($num > $perpage) {
		$page = 10;
		$offset = 2;
		$pages = ceil($num / $perpage);
		$from = $curr_page - $offset;
		$to = $curr_page + $page - $offset - 1;
		if($page > $pages) {
			$from = 1;
			$to = $pages;
		} else {
			if($from < 1) {
				$to = $curr_page + 1 - $from;
				$from = 1;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$to = $page;
				}
			} elseif($to > $pages) {
				$from = $curr_page - $pages + $to;
				$to = $pages;
				if(($to - $from) < $page && ($to - $from) < $pages) {
					$from = $pages - $page + 1;
				}
			}
		}
		$multipage .= '<div class="acfy"><ul style="margin-left:12px;">';
		if($curr_page>1){
			$multipage .='<li><a href="'.$mpurl.'1"><img src="/images/la1.gif" alt="首页"></a></li>';
			$multipage .='<li><a href="'.$mpurl.($curr_page-1).'"><img src="/images/la2.gif" alt="上一页"></a></li>';
		}
		for($i = $from; $i <= $to; $i++) {
			if($i != $curr_page) {
				$multipage .= '<li class="p1"><a href="'.$mpurl.$i.'">'.$i.'</a></li>';
			} else {
				$multipage .= '<li class="p2" title="本页">'.$i.'</li>';			
			}
		}
		if($curr_page<$pages){
			$multipage .='<li><a href="'.$mpurl.($curr_page+1).'"><img src="/images/ra1.gif" alt="下一页"></a></li>';
			$multipage .='<li><a href="'.$mpurl.$pages.'"><img src="/images/ra2.gif" alt="尾页"></a></li>';
		}
		$multipage .= '</ul></div>';
	}
	return $multipage;
}

?>